package com.tanhua.server.service;

import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.*;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UserLocationApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TodayBestService {

    @Reference
    private RecommendUserApi recommendUserApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private QuestionApi questionApi;

    @Reference
    private UserLocationApi userLocationApi;

    //查询缘分值最高的用户业务
    public ResponseEntity<Object> getTodayBest() throws InvocationTargetException, IllegalAccessException {
        Long userId = UserHolder.getUserId();
        RecommendUser recommendUser = recommendUserApi.queryWithMaxScore(userId);
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setRecommendUserId(2L);
            recommendUser.setScore(80D);
        }
        UserInfo userInfo = userInfoApi.findById(Long.valueOf(recommendUser.getUserId()));
        TodayBestVo todayBestVo = new TodayBestVo();

        if (userInfo != null) {
            BeanUtils.copyProperties(todayBestVo, userInfo);

            if (userInfo != null) {
                String tags = userInfo.getTags();
                String[] split = tags.split(",");
                todayBestVo.setTags(split);
            }

            todayBestVo.setFateValue(recommendUser.getScore().longValue());
        }
        return ResponseEntity.ok(todayBestVo);
    }

    //交友功能首页推荐列表分页业务
    public ResponseEntity<Object> recommendation(RecommendQueryVo vo) {
        Long userId = UserHolder.getUserId();
        PageResult pageResult = recommendUserApi.queryRecommendation(userId, vo.getPage(), vo.getPagesize());

        List<RecommendUser> items = (List<RecommendUser>) pageResult.getItems();
        if(items == null){
            items = new ArrayList<>();
            for (long i = 0; i < 5; i++) {
                RecommendUser recommendUser = new RecommendUser();
                recommendUser.setUserId(i);
                recommendUser.setScore(70D + i);
                items.add(recommendUser);
            }
        }
        List<TodayBestVo> result = new ArrayList<>();
        items.forEach(s->{
            TodayBestVo todayBestVo = new TodayBestVo();
            UserInfo userInfo = userInfoApi.findById(s.getRecommendUserId());
            if(userInfo != null){
                try {
                    BeanUtils.copyProperties(todayBestVo, userInfo);
                    if(userInfo.getTags() != null){
                        todayBestVo.setTags(userInfo.getTags().split(","));;
                    }
                    todayBestVo.setFateValue(s.getScore().longValue());
                    result.add(todayBestVo);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        });

        pageResult.setItems(result);

        return ResponseEntity.ok(pageResult);
    }

    //推荐用户个人信息业务
    public ResponseEntity<Object> queryPersonalInfo(Long id) throws InvocationTargetException, IllegalAccessException {
        UserInfo userInfo = userInfoApi.findById(id);
        TodayBestVo vo = new TodayBestVo();

        if(userInfo != null){
            BeanUtils.copyProperties(vo,userInfo);
            if(userInfo.getTags() != null){
                vo.setTags(userInfo.getTags().split(","));
                long score = recommendUserApi.queryScore(UserHolder.getUserId(),id);
                vo.setFateValue(score);
            }

        }
        return ResponseEntity.ok(vo);
    }

    //查询陌生人问题业务
    public ResponseEntity<Object> showStrangeQuestions(Long userId) {
        Question question = questionApi.findByUid(userId);
        String txt = question != null ? question.getTxt() : "你喜欢什么？";
        return ResponseEntity.ok(txt);
    }

    //搜附近的人业务
    public ResponseEntity<Object> searchNearBy(String gender, Long distance) throws InvocationTargetException, IllegalAccessException {
        Long userId = UserHolder.getUserId();
        List<UserLocationVo> list = userLocationApi.searchNearBy(distance, userId);

        List<SearchVo> searchVoList = new ArrayList<>();

        if(list != null){
            for (UserLocationVo locationVo : list) {
                if(locationVo.getUserId() == userId){
                    continue;
                }

                UserInfo userInfo = userInfoApi.findById(locationVo.getUserId());
                if(userInfo.getGender().equals(gender)){
                    SearchVo vo = new SearchVo();
                    BeanUtils.copyProperties(vo,userInfo);
                    searchVoList.add(vo);
                }

            }
        }
        return ResponseEntity.ok(searchVoList);
    }
}
